package Common;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XMLParser {
	
	/** The function to parse the content of input stream into array of articles
	 * @param Is: the input stream
	 * @param Res: the array of articles parsed
	 * @return true if parse correctly, false if encountering some exception
	 */
	
	static protected int parse(InputStream Is, ArrayList<Article>Res) {
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
		int NumRes = 0;
		try{
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			//InputStream is = new FileInputStream("bin/response.xml");
			Document doc = dombuilder.parse(Is);
			Element root = doc.getDocumentElement();
			NodeList resultset_web = root.getElementsByTagName("resultset_web");
			Node resultset = resultset_web.item(0);
			
			String count = resultset.getAttributes().getNamedItem("count").getNodeValue();
			NumRes = Integer.parseInt(count);
			
			
			NodeList results = resultset.getChildNodes();  
			if(results != null)
			{			
				//System.out.println("results length: "+results.getLength());
				for(int i = 0 ; i < results.getLength(); i++){
					Node result = results.item(i);								
					Article newArticle = new Article();
					
					if(result.getNodeType()==Node.ELEMENT_NODE){
						//System.out.println("result #" + NumResults++ + ":");
						for(Node node = result.getFirstChild(); node != null; node = node.getNextSibling()){
							if(node.getNodeType()==Node.ELEMENT_NODE){														
								if(node.getNodeName().equals("title") && node.getFirstChild() != null){
									
									String temp = node.getFirstChild().getNodeValue();
									temp = temp.replaceAll("<b>", "");
									String title = temp.replaceAll("</b>", "");
									newArticle.setTitle(title);
									//System.out.println("title: " + title);									
								}
								if(node.getNodeName().equals("abstract") && node.getFirstChild() != null){
									//idx++;
									String temp = node.getFirstChild().getNodeValue();
									temp = temp.replaceAll("<b>","");
									String abs = temp.replaceAll("</b>", "");
									newArticle.setAbstract(abs);
									//System.out.println("abstract: " + abs);								
								}
								if(node.getNodeName().equals("url") && node.getFirstChild() != null){
									String url = node.getFirstChild().getNodeValue();
									newArticle.setURL(url);
									//System.out.println("url: " + url);								
								}
							}
						}
					}
					if(newArticle.title != null)
						Res.add(newArticle);
				}
			}
			
		}catch(ParserConfigurationException e){
			e.printStackTrace();
			return -1;
		}catch(SAXException e){
			e.printStackTrace();
			return -1;
		}catch(IOException e){
			e.printStackTrace();
			return -1;
		}
		
		return NumRes;
	}
	
	
}
